/**
 * Created by admin on 2017/4/10.
 */
define(['app', 'filters/filters'],function(app) {
    app.register.controller('nenglitest', function ($scope, $http, $state, $sce, $stateParams, $rootScope, $confirm, $cookieStore) {
        $scope.loading_show = true;
        if($cookieStore.get("cate_id")){
            $rootScope.cate_id_nengli = $cookieStore.get("cate_id");
        }
        if($rootScope.cate_id_nengli==1){
            $scope.cate = "运营"
        }
        if($rootScope.cate_id_nengli==2){
            $scope.cate = "客服"
        }
        if($rootScope.cate_id_nengli==3){
            $scope.cate = "美工"
        }
        if($rootScope.cate_id_nengli==4){
            $scope.cate = "营销"
        }
        $scope.duo_show = false;//默认显示单选页
        // $scope.dagou_duo = false;//默认多选页不打钩
        $scope.color = false;

        //图片放大
        $scope.img_big=false;
        $scope.img_click = function(img){
            $scope.img_big=!$scope.img_big;
            $scope.scaleImg=img;
            var imgObj = new Image();
            imgObj.src = img; //若是jq，则直接将此代码换成 imgObj.src = _test.attr("src"); 即可。
            var k = document.body.clientWidth/2-imgObj.width/2;
            var v = document.body.clientHeight/2-imgObj.height/2;
            $(".bigImgBox").css("left",k+'px');
            $(".bigImgBox").css("top",v+'px');
            $scope.obj_img = {
                left:k,
                top:v,
                width:imgObj.width,
                height:imgObj.height
            }
            $(".bigImgBox").css("left",$scope.obj_img.left+'px');
            $(".bigImgBox").css("top",$scope.obj_img.top+'px');
            $('.bigImgBox .bigImg').css({'width':$scope.obj_img.width,"height":$scope.obj_img.height});
        }
        //图片放大结束
        var divImg = document.getElementsByClassName('bigImgBox');
        divImg[0].addEventListener("DOMMouseScroll",function(e){
            var imgWidth = $('.bigImgBox .bigImg').width();
            var imgHeight = $('.bigImgBox .bigImg').height();
            var imgLeft = divImg[0].offsetLeft;
            var imgTop = divImg[0].offsetTop;
            //console.log(e)
            if(e.detail > 0){
                imgLeft = imgLeft-imgWidth/20;
                imgTop = imgTop-imgHeight/20;
                imgWidth = imgWidth+imgWidth/10;
                imgHeight = imgHeight+imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
            if(e.detail < 0){
                imgLeft = imgLeft+imgWidth/20;
                imgTop = imgTop+imgHeight/20;
                imgWidth = imgWidth-imgWidth/10;
                imgHeight = imgHeight-imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
        });

        divImg[0].addEventListener('mousewheel',function(e){
            var imgWidth = $('.bigImgBox .bigImg').width();
            var imgHeight = $('.bigImgBox .bigImg').height();
            var imgLeft = divImg[0].offsetLeft;
            var imgTop = divImg[0].offsetTop;
            if(e.deltaY > 0 || e.wheelDelta < 0){
                imgLeft = imgLeft-imgWidth/20;
                imgTop = imgTop-imgHeight/20;
                imgWidth = imgWidth+imgWidth/10;
                imgHeight = imgHeight+imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
            if(e.deltaY < 0|| e.wheelDelta > 0){
                imgLeft = imgLeft+imgWidth/20;
                imgTop = imgTop+imgHeight/20;
                imgWidth = imgWidth-imgWidth/10;
                imgHeight = imgHeight-imgHeight/10;
                $('.bigImgBox').css({'left':imgLeft,'top':imgTop})
                $('.bigImgBox .bigImg').css({'width':imgWidth,"height":imgHeight});
            }
        })

//
        $scope.nengli_over = false;
        //请求试卷页面
        //$scope.postId = JSON.parse($cookieStore.get("userInfo")).post_id;//获取岗位id
        $http({
            url: app._baseUrl + '/Qy/AbilityCheck/questionList',//测评列表
            method: 'POST',
            data:{
                //cate_id:$scope.postId
                cate_id:$rootScope.cate_id_nengli
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function(res){
            if(res.success){//请求成功
                $scope.loading_show = false;
                $scope.data = res.data;
                $scope.count_all = $scope.data.simple_count+$scope.data.multiple_count;
                $scope.simple_list = $scope.data.simple_list//单选列表
                $scope.multiple_list = $scope.data.multiple_list//多选列表
                //单选点击事件
                $scope.arr_save = [];
                $scope.danxuan_click = function(index,listindex,ques_id2){
                    $scope.arr_save[listindex] = {
                        ques_id:ques_id2,
                        temp:index
                    }
                }
            }
        })
        $scope.list = [];
        //下一页点击事件
        $scope.next_duo = function(){
            //单选判断有没有选择
            for(var i = 0;i<$scope.simple_list.length;i++){
                if(!$('.concent_nl .dan_list').eq(i).find('li').hasClass('dagou')){
                    $scope.ques_id2 = $scope.simple_list[i].question_id;
                    $scope.arr_save[i] = {//将此题信息保存
                        ques_id:$scope.ques_id2,
                        // temp:3//4
                        temp:5
                    }
                }
            }
            for(var i = 0;i<$scope.arr_save.length;i++){
                if($scope.arr_save[i].temp!=''||$scope.arr_save[i].temp==0){
                    $scope.index = aBcd($scope.arr_save[i].temp);
                    $scope.simple_obj = {//题目、答案传过去形式
                        question_id:$scope.arr_save[i].ques_id,
                        s_answer:$scope.index
                    };
                    $scope.list.push($scope.simple_obj);
                }
            }
            $scope.duo_show = true;
            //console.log($scope.arr_save)
        }
        //上一页
        $scope.prev_dan = function(){
             $scope.list = [];
             $scope.duo_show = false;
        }
        //多选
        $scope.arr_save_xuanxiang = [];
        $scope.duo_xuan = function(par_index,index,color,ques_id2){
            index = aBcd(index);
           if(!$scope.arr_save_xuanxiang[par_index]){//做的是新的一道多选题时
               if(color==true){
                   $scope.arr_save_xuanxiang[par_index] = {//将此题信息保存
                       ques_id:ques_id2,
                       temp:[index]
                   }
                   //console.log($scope.arr_save_xuanxiang[par_index].temp)
               }
           }else{//还是选择同一题
               $scope.arr_save_xuanxiang[par_index].temp = $scope.arr_save_xuanxiang[par_index].temp.split(',');
               //console.log($scope.arr_save_xuanxiang[par_index].temp)
               if(color==true){
                   $scope.arr_save_xuanxiang[par_index].temp.push(index);
               }else{
                   $scope.arr_save_xuanxiang[par_index].temp.remove(index);
               }
               var len = $scope.arr_save_xuanxiang[par_index].temp.length;
               for(var i = 0; i < len; i++) {
                   if($scope.arr_save_xuanxiang[par_index].temp[i] == '') $scope.arr_save_xuanxiang[par_index].temp.splice(i,1);
               }
               //console.log($scope.arr_save_xuanxiang[par_index].temp)
           }
           //排序
            var arr1 = $scope.arr_save_xuanxiang[par_index].temp;
            $scope.arr_save_xuanxiang[par_index].temp = arr1.sort().join(',');
            //console.log($scope.arr_save_xuanxiang[par_index].temp)
    }
        //提交试卷
        // $scope.list = [];
        $scope.tijiao_t = true;
        $scope.sign_up = function(){
            $scope.ti_jiao = false;
            //多选判断有没有选择
            for(var i = 0;i<$scope.multiple_list.length;i++){
                if(!$('.concent_nl .duo_list').eq(i).find('li').hasClass('dagou_duo')){
                    $scope.ques_id2 = $scope.multiple_list[i].question_id;
                    $scope.arr_save_xuanxiang[i] = {//将此题信息保存
                        ques_id:$scope.ques_id2,
                        temp:0
                    }
                }

            }
            if($scope.tijiao_t==true){
                $scope.tijiao_t = false;
                for(var i = 0;i<$scope.arr_save_xuanxiang.length;i++){
                    // if($scope.arr_save_xuanxiang[i].temp!=''){
                        $scope.simple_obj = {//题目、答案传过去形式
                            question_id:$scope.arr_save_xuanxiang[i].ques_id,
                            s_answer:$scope.arr_save_xuanxiang[i].temp
                        };
                        $scope.list.push($scope.simple_obj);
                    // }
                }
                // console.log($scope.simple_obj)
                $scope.list = JSON.stringify($scope.list);
                //请求提交答案接口
                $http({
                    url: app._baseUrl + '/Qy/AbilityCheck/answerCheckList',//答案
                    method: 'POST',
                    data:{
                        // cate_id:$scope.postId,
                        cate_id:$rootScope.cate_id_nengli,
                        question_list:$scope.list
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).success(function(res){
                    if(res.success){
                        //tanchaung
                        $scope.nengli_over = true;
                        $scope.check_total_score = res.data.check_total_score;
                        $scope.recmd_cate = res.data.recmd_cate;
                        $scope.know_over = function(){
                            history.back();
                        }
                    }
                })
            }
        }
        Array.prototype.remove = function(val) {//数组去掉特定项
            var index = this.indexOf(val);
            if (index > -1) {
                this.splice(index, 1);
            }
        };
        function aBcd(index){
            if(index==0){
               return 'A';
            }
            if(index==1){
                return 'B';
            }
            if(index==2){
                return 'C';
            }
            if(index==3){
                return 'D';
            }
            if(index==4){
                return 'E';
            }
            if(index==5){
                return '0'
            }
        }
        //中断弹窗
        $rootScope.show_nl_tc = false;
        $cookieStore.put('nl_tc',1);
        $scope.goState = function(){
            $cookieStore.put('nl_tc',1);
            $rootScope.show_nl_tc = false;
        }
        $scope.goYes = function(){
            $cookieStore.put('nl_tc',0);
            if($rootScope.hash=='app.compProfession'){
                $state.go('app.compProfession');
            }
            if($rootScope.hash=='app.myCourse'){
                // $state.go('app.myCourse');
                $state.go('app.myMajor');
            }
            if($rootScope.hash=='app.proIndex'){
                $state.go('app.proIndex');
            }
        }
        //提交弹窗
        $scope.ti_jiao = false;
        $scope.sign_up_tc = function(){
            $scope.ti_jiao = true;
        }
        $scope.ti_jiao_quxiao = function(){
            $scope.ti_jiao = false;
        }
    })
})